package com.itcat.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.SmsHomeRecommendProductApi;
import com.itcat.mapper.SmsHomeRecommendProductMapper;
import com.itcat.mysql.pojo.SmsHomeNewProduct;
import com.itcat.mysql.pojo.SmsHomeRecommendProduct;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeRecommendProductApiImpl implements SmsHomeRecommendProductApi {

    @Autowired
    private SmsHomeRecommendProductMapper smsHomeRecommendProductMapper;
    @Override
    public Page<SmsHomeRecommendProduct> list(String productName, Integer recommendStatus, Integer pageSize, Integer pageNum) {
        Page<SmsHomeRecommendProduct> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeRecommendProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(productName != null, SmsHomeRecommendProduct::getProductName, productName)
                .eq(recommendStatus != null, SmsHomeRecommendProduct::getRecommendStatus, recommendStatus);
        Page<SmsHomeRecommendProduct> productPage = smsHomeRecommendProductMapper.selectPage(page, wrapper);
        return productPage;
    }

    @Override
    public void create(List<SmsHomeRecommendProduct> smsHomeRecommendProducts) {
        for (SmsHomeRecommendProduct smsHomeRecommendProduct : smsHomeRecommendProducts) {
            smsHomeRecommendProductMapper.insert(smsHomeRecommendProduct);
        }
    }

    @Override
    public void delete(List<Integer> ids) {
        smsHomeRecommendProductMapper.deleteBatchIds(ids);

    }

    @Override
    public void updateRecommendStatus(List<Integer> ids, Integer recommendStatus) {
        LambdaUpdateWrapper<SmsHomeRecommendProduct> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(SmsHomeRecommendProduct::getId,ids).set(SmsHomeRecommendProduct::getRecommendStatus,recommendStatus);
        SmsHomeRecommendProduct smsHomeRecommendProduct = new SmsHomeRecommendProduct();
        smsHomeRecommendProduct.setRecommendStatus(recommendStatus);
        smsHomeRecommendProductMapper.update(smsHomeRecommendProduct,updateWrapper);
    }

    @Override
    public void updateSort(Integer id, Integer sort) {
        SmsHomeRecommendProduct smsHomeRecommendProduct = new SmsHomeRecommendProduct();
        smsHomeRecommendProduct.setId(id.longValue());
        smsHomeRecommendProduct.setSort(sort);
        smsHomeRecommendProductMapper.updateById(smsHomeRecommendProduct);
    }
}
